package cn.iocoder.yudao.module.erp.dal.mysql.supplierfleet;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.dal.dataobject.supplierfleet.SupplierFleetDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.erp.controller.admin.supplierfleet.vo.*;
import java.util.List;

/**
 * 供应商车队信息 Mapper
 * @author 天使管理员
 */
@Mapper
public interface SupplierFleetMapper extends BaseMapperX<SupplierFleetDO> {

    default PageResult<SupplierFleetDO> selectPage(SupplierFleetPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SupplierFleetDO>()
                .eqIfPresent(SupplierFleetDO::getType, reqVO.getType())
                .likeIfPresent(SupplierFleetDO::getName, reqVO.getName())
                .eqIfPresent(SupplierFleetDO::getFleet, reqVO.getFleet())
                .eqIfPresent(SupplierFleetDO::getIsShow, reqVO.getIsShow())
                .eqIfPresent(SupplierFleetDO::getInfo, reqVO.getInfo())
                .orderByDesc(SupplierFleetDO::getId));
    }

    default List<SupplierFleetDO> supplierList(String name,Integer isShow){
        QueryWrapper<SupplierFleetDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("MIN(id) AS id,name,MAX(currency_type) AS currencyType,SUM(total_amount)-SUM(paid_amount) AS outAmount,SUM(total_amount) AS totalAmount,SUM(paid_amount) AS paidAmount,MAX(create_time) AS createTime,MAX(category) AS category");
        queryWrapper.groupBy("name");
        if(name!=null&&name!=""){
            queryWrapper.like("name", name);
        }
        if(isShow!=null){
            queryWrapper.eq("is_show", isShow);
        }else {
            queryWrapper.ne("is_show", 1);
        }
        return selectList(queryWrapper);
    }

    default List<SupplierFleetDO> fleetList(String name,String fleet){
        QueryWrapper<SupplierFleetDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",1);
        if(fleet!=null&&fleet!=""){
            queryWrapper.like("fleet", fleet);
        }
        if(name!=null&&name!=""){
            queryWrapper.eq("name", name);
        }
        queryWrapper.select("MIN(id) AS id,MIN(currency_type) AS currencyType,MAX(name) AS name,SUM(total_amount)-SUM(paid_amount) AS outAmount,MAX(is_show) AS isShow,MAX(currency_type) AS currencyType,MAX(category) AS category,fleet,SUM(total_amount) AS totalAmount,SUM(paid_amount) AS paidAmount,MAX(info) AS info,MAX(create_time) AS createTime");
        queryWrapper.groupBy("fleet");
        return selectList(queryWrapper);
    }

    default List<SupplierFleetDO> fleetPayList(String name,String fleet){
        QueryWrapper<SupplierFleetDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        queryWrapper.eq("fleet",fleet);
        queryWrapper.isNotNull("info");
        queryWrapper.orderByDesc("id");
        return selectList(queryWrapper);
    }

    default List<SupplierFleetDO> totalMoney(String name,String fleet,Integer isShow){
        QueryWrapper<SupplierFleetDO> queryWrapper = new QueryWrapper<>();
        if(fleet!=null&&fleet!=""){
            queryWrapper.eq("fleet", fleet);
        }
        if(name!=null&&name!=""){
            queryWrapper.eq("name", name);
        }
        if(isShow!=null){
            queryWrapper.eq("is_show", isShow);
        }else {
            queryWrapper.ne("is_show", 1);
        }
        queryWrapper.select("currency_type AS currencyType,SUM(total_amount) AS totalAmount,SUM(paid_amount) AS paidAmount,SUM(total_amount)-SUM(paid_amount) AS outAmount");
        queryWrapper.groupBy("currency_type");
        return selectList(queryWrapper);
    }

}